﻿Imports System.Windows.Forms
Imports TyranntEngine

Public Class LogonControl
    Private WithEvents _client As New TyranntUserService.TyranntUserServiceClient
    Private _token As New TyranntUserService.TokenType

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.Text = "Please wait ... Checking"
        EnableForm(False)

        Dim loginData As New TyranntUserService.LoginType
        loginData.Username = usernameTextbox.Text
        loginData.Password = EncryptPassword(passwordTextbox.Text)
        passwordTextbox.Text = ""
        _client.LoginAsync(loginData)
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub usernameTextbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles usernameTextbox.TextChanged
        CheckLoginButton()
    End Sub

    Private Sub passwordTextbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles passwordTextbox.TextChanged
        CheckLoginButton()
    End Sub

    Private Sub CheckLoginButton()
        If usernameTextbox.Text = "" Or passwordTextbox.Text = "" Then
            OK_Button.Enabled = False
        Else
            OK_Button.Enabled = True
        End If
    End Sub

    Private Sub _client_LoginCompleted(ByVal sender As Object, ByVal e As TyranntUserService.LoginCompletedEventArgs) Handles _client.LoginCompleted
        _token = e.Result

        If _token.TokenString.StartsWith("[#]") Then
            If _token.TokenString.StartsWith("[#]failed[") Then
                MessageBox.Show(_token.TokenString, "Fatal Logon Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf _token.TokenString.StartsWith("[#]usernotexist") Then
                Me.Text = "Login Failed, Try Again"
            Else
                MessageBox.Show(_token.TokenString, "Unknown Error Code", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
            EnableForm(True)
        Else
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            EnableForm(True)
            Me.Close()
        End If

    End Sub

    Private Sub EnableForm(ByVal enabled As Boolean)
        usernameTextbox.Enabled = enabled
        passwordTextbox.Enabled = enabled
        OK_Button.Enabled = enabled
    End Sub

    Public ReadOnly Property UserAlias() As String
        Get
            Return _token.UserAlias
        End Get
    End Property

    Public ReadOnly Property UserGroup() As Integer
        Get
            Return _token.UserGroup.IntegerValue
        End Get
    End Property

    Public ReadOnly Property TokenString() As String
        Get
            Return _token.TokenString
        End Get
    End Property

    Private Sub registerButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = Windows.Forms.DialogResult.Ignore
        Me.Close()
    End Sub
End Class
